﻿using AutoMapper;
using B.S.Sterilizationcenter.Domain.zmc;
using B.S.Sterilizationcenter.ErrorCode;
using B.S.Sterilizationcenter.Read.API.Appliaction.Commends.zmc.Instrumentkit;
using B.S.Sterilizationcenter.Read.API.Dto;
using MediatR;
using SqlSugar;
using System.Diagnostics.Metrics;

namespace B.S.Sterilizationcenter.Read.API.Appliaction.CommandHandlers.zmc.Instrumentkit
{
    public class QueryInstrumentkitCommendHandlers : IRequestHandler<QueryInstrumentkitCommends, ApiResult<List<InstrumentkitApparaDtos>>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;

        public QueryInstrumentkitCommendHandlers(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }


        /// <summary>
        /// 查询器械包数据
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult<List<InstrumentkitApparaDtos>>> Handle(QueryInstrumentkitCommends request, CancellationToken cancellationToken)
        {
            ApiResult<List<InstrumentkitApparaDtos>> result = new ApiResult<List<InstrumentkitApparaDtos>>();
            result.Code = ApiEnums.查询成功;
            result.Msg = "查询成功";
            var data= db.Queryable<Instrumentkits,Apparatus>((a,b)=>a.InstrumentkitsNo==b.ApparatusNo).Where(a=>a.IsDeletes==false ).OrderBy((a,b)=>a.IsDeletes).Select((a,b)=>new InstrumentkitApparaDtos
            {

                InstrumentkitsId=a.InstrumentkitsId,
                InstrumentkitsName=a.InstrumentkitsName,
                InstrumentkitsNo=a.InstrumentkitsNo,
                InstrumentkitsType=a.InstrumentkitsType,
                Department=a.Department,
                Sterilizamethod=a.Sterilizamethod,
                Outerpackags=a.Outerpackags,
                Effectdays=a.Effectdays,
                Serviceprice=a.Serviceprice,
                State=a.State,
                IsDeletes=a.IsDeletes,
          /*      Durableproperties=b.Durableproperties,
                ApparatusName =b.ApparatusName,*/
                ApparatusNo=b.ApparatusNo,
                

            }).ToList();

            if (!string.IsNullOrEmpty(request.InstrumentkitsName)) {
                data= data.Where(p => p.InstrumentkitsName.Contains(request.InstrumentkitsName)).ToList();
            }
            if (!string.IsNullOrEmpty(request.InstrumentkitsNo)) {
                data= data.Where(p => p.InstrumentkitsNo.Contains(request.InstrumentkitsNo)).ToList();
            }
       

            result.Data = data;
            return result;
        }
    }
}
